<?php

function getChildren($uid)
{
    return T("parents_relationship")->fetch_by_uid($uid);
}

/**
 * uid是否是targetid的孩子
 *
 * @param int $uid
 * @param int $targetid
 */
function isChild($uid, $parentid)
{
    return T("parents_relationship")->contain_child($uid, $parentid);
}

function addChild($account, $passwd, $parentid, $relationship)
{
    $profile = getProfile($parentid);
    if (! $profile) {
        return - 10;
    }
    if ($profile["user_type"] == 0) {
        return - 9;
    }
    $uid = getUID($account);
    if ($uid == - 1) {
        return - 1;
    }
    $profile = getProfile($uid);
    if ($profile["user_type"] != 0) {
        return - 11;
    }
    if (isChild($uid, $parentid)) {
        return - 8;
    }

    if (! judzePassword($account, $passwd, $uid)) {
        return - 2;
    }
    T("parents_relationship")->insert(array(
        "uid" => $parentid,
        "childid" => $uid,
        "relationship" => $relationship
    ), array(
        "uid",
        "childid"
    ));
    return 0;
}

function removeShip($uid, $childid)
{
    return T("parents_relationship")->remove("`uid`=$uid AND `childid`=$childid");
}

?>